from django.test import TestCase
from rest_framework.test import APITestCase
from rest_framework import status
from client.models import UserAccount

# Create your tests here.
class ManageTests(APITestCase):
    def setUp(self):
        self.email = 'some@resporter.app'
        self.password = 'qqq111!!!'
        self.user = UserAccount.objects.create_user(email=self.email, password=self.password)
        self.response=self.client.post('/api/token/login/', {
            'email':self.email,
            'password':self.password
            },format='json')
        self.assertEqual(self.response.status_code, status.HTTP_200_OK)
        self.auth_token=self.response.data['auth_token']
        
    def test_create_project(self):
        test_create_data={
            'user_id':1,
            'title':'项目0',
            'introduction':'...',
            'type':1,
            'isCollect':False,
            'isDelete':False
        }
        # TEST:Create a project when it is not authorized
        response=self.client.post('/api/project-manage/create/',test_create_data,format='json')
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
        # TEST:Create a project when it is authorized
        response=self.client.post('/api/project-manage/create/',test_create_data,format='json',HTTP_AUTHORIZATION='Token ' + self.auth_token)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
    
    def test_get_project(self):
        showType='All'
        # TEST:Get projects when it is not authorized
        response=self.client.get('/api/project-manage/get-projects/?showType='+showType)
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
        # TEST:Get projects when it is authorized
        response=self.client.get('/api/project-manage/get-projects/?showType='+showType,HTTP_AUTHORIZATION='Token ' + self.auth_token)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_collect_project(self):
        test_create_data={
            'user_id':1,
            'title':'项目0',
            'introduction':'...',
            'type':1,
            'isCollect':False,
            'isDelete':False
        }
        # TEST:Create a project when it is authorized
        response=self.client.post('/api/project-manage/create/',test_create_data,format='json',HTTP_AUTHORIZATION='Token ' + self.auth_token)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        
        test_collect_data={
            'project_id':1
        }
        # TEST:Collect a project when it is not authorized
        response=self.client.put('/api/project-manage/collect/',test_collect_data,format='json')
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
        # TEST:Collect a project when it is authorized
        response=self.client.put('/api/project-manage/collect/',test_collect_data,format='json',HTTP_AUTHORIZATION='Token ' + self.auth_token)
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

    def test_delete_project(self):
        test_create_data={
            'user_id':1,
            'title':'项目0',
            'introduction':'...',
            'type':1,
            'isCollect':False,
            'isDelete':False
        }
        # TEST:Create a project when it is authorized
        response=self.client.post('/api/project-manage/create/',test_create_data,format='json',HTTP_AUTHORIZATION='Token ' + self.auth_token)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        test_delete_data={
            'project_id':1
        }
        # TEST:Delete a project when it is not authorized
        response=self.client.post('/api/project-manage/delete/',test_delete_data,format='json')
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
        # TEST:Delete a project when it is authorized
        response=self.client.post('/api/project-manage/delete/',test_delete_data,format='json',HTTP_AUTHORIZATION='Token ' + self.auth_token)
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

    def test_modify_project(self):
        test_create_data={
            'user_id':1,
            'title':'项目0',
            'introduction':'...',
            'type':1,
            'isCollect':False,
            'isDelete':False
        }
        # TEST:Create a project when it is authorized
        response=self.client.post('/api/project-manage/create/',test_create_data,format='json',HTTP_AUTHORIZATION='Token ' + self.auth_token)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        test_modify_data={
            'project_id':1,
            'title':'项目1',
            'introduction':'....'
        }
        # TEST:Modify a project when it is not authorized
        response=self.client.put('/api/project-manage/modify/',test_modify_data,format='json')
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
        # TEST:Modify a project when it is authorized
        response=self.client.put('/api/project-manage/modify/',test_modify_data,format='json',HTTP_AUTHORIZATION='Token ' + self.auth_token)
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

    def test_search_project(self):
        searchContent='项目'
        searchType='全部'
        # TEST:Search a project when it is not authorized
        response=self.client.get('/api/project-manage/search/?searchContent='+searchContent+'&searchType='+searchType)
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
        # TEST:Search a project when it is authorized
        response=self.client.get('/api/project-manage/search/?searchContent='+searchContent+'&searchType='+searchType,HTTP_AUTHORIZATION='Token ' + self.auth_token)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
